var prefix = "/jzweb/eventresourceAllocation";
$(function () {
    getIdList();
    getYearList()

});
$().ready(function () {
    $('#fileProgress').hide()
    $('#yearOld').val($('#yearOldvalue').val())
    $("#year").val($("#years").val())
    $("#jzEveMoId").val($("#jzEveMoIds").val())
    $("#eventType").val($("#eventTypeTs").val());
    $("#jianjievalues").val($("#jianjievaluesvalue").val());
    $('#grade').val($('#gradeTs').val())

    if ($('#jzEveMoIds').val() != 1775402446075265026) {
        $('#yearbox').hide()
        $('#eventTypebox').hide()
        $('#year').val('')
        $('#eventType').val('')
    }
    if ($('#jzEveMoIds').val() == 1775402577172430850) {
        $('#grade').show()
        $('#gradebox').show()
    } else {
        $('#gradebox').hide()
        $('#grade').hide()
        $('#grade').val('')
    }
    if ($('#jzEveMoIds').val() == 1775402630247153666) {
        $('#jianjie').show()
        $('#yearOlds').show()
        $('#suoShu').hide()
        $('#eventTitle').val('')
        $('#eventTitle').prop('disabled', true);
        $('#mp4url').prop('disabled', false);
        $('#yearOld').prop('disabled', false);
    } else {
        $('#suoShu').show()
        $('#yearOlds').hide()
        $('#yearOld').val('')
        $('#jianjie').hide()
        $('#eventTitle').prop('disabled', false);
        $('#mp4url').prop('disabled', true);
        $('#yearOld').prop('disabled', true);
    }
    if($('#jzEveMoIds').val() == 1775402307172499457 || $('#jzEveMoIds').val() == 1775402630247153666){
        $('#jianjie').show()
    }else {
        $('#jianjie').hide()
    }
    validateRule();
});

$.validator.setDefaults({
    submitHandler: function () {
        update();
    }
});

layui.use(['form', 'upload','element'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var upload = layui.upload;
    var element=layui.element;
    var loading;

    $("select[name='position']").val($("#position").val())
    form.render('select');
    upload.render({
        elem: '#videobtn'
        , url: '/common/sysFile/ftpUpload'
        , accept: 'video', //视频
        exts: 'mp3|mp4'
        ,
        choose: function (res) {
            $("#mp4url").val('正在上传中，请稍后...');
            $("#mp4url").prop('disabled', true);
        },
        done: function (res) {
            console.log(res)
            $("#mp4url").val(res.data);
            $("#mp4url").prop('disabled', false);
        },
        progress: function (e, percent) {
            $("#fileProgress").show();
            element.progress('progressBar', e + '%');
            // if (e != 100) {
            //     index = layer.load(1, {
            //         shade: [0.1, '#fff'],//0.1透明度的白色背景
            //         content: '请稍候，正在上传文件...',
            //         success: function (layerContentStyle) {
            //             layerContentStyle.find('.layui-layer-content').css({
            //                 'padding-top': '35px',
            //                 'text-align': 'left',
            //                 'width': '300px'
            //             });
            //         }
            //     });
            // }
        },
    });
    //拖拽上传
    upload.render({
        elem: '#drag'
        , url: '/common/sysFile/upload' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
        // ,field:'picture'
        , before: function () {
            loading = layer.load({
                type: 3,
                content: '加载中...',
            });//在post之前定义loading
        }
        , done: function (res) {
            $('#img').val(res.data)
            layer.close(loading);
            if (res.code == 0) {
                layer.msg('上传成功');
                layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.data);
                $("input[name='picture']").val(res.data)
            } else {
                layer.msg('上传失败,请检查');
            }
        }
    });

    form.verify({
        name: function (value) {
            if (!value) {
                return '请输入图片名称'
            }
        },
        picture: function (value) {
            if (!value) {
                return '请上传图片'
            }
        }
    })
});

function getIdList() {
    var idlist = []
    var val = $("#jzEveMoIds").val()
    $.ajax({
        url: prefix + "/getModelIds",
        type: "get",
        success: function (r) {
            idlist = r.data
            if (idlist.length == 0) {
                $("#jzEveMoId").append('<option value="">暂无数据</option>');
            } else {
                $("#jzEveMoId").append('<option value="">请选择</option>');
            }
            for (var i in idlist) {
                if (val != i) {
                    $("#jzEveMoId").append('<option value="' + i + '">' + idlist[i] + '</option>');
                } else {
                    $("#jzEveMoId").append('<option selected value="' + i + '">' + idlist[i] + '</option>');
                }
            }
        }
    });
}

// 获取年份列表
function getYearList() {
    var yearlist = []
    var val = $("#years").val()
    $.ajax({
        url: prefix + "/getYearList",
        type: "get",
        success: function (r) {
            yearlist = r.data
            if (yearlist.length == 0) {
                $("#year").append('<option value="">暂无数据</option>');
            } else {
                $("#year").append('<option value="">请选择</option>');
            }
            for (var i in yearlist) {
                if (val != yearlist[i]) {
                    $("#year").append('<option value="' + yearlist[i] + '">' + yearlist[i] + '</option>');
                } else {
                    $("#year").append('<option selected value="' + yearlist[i] + '">' + yearlist[i] + '</option>');
                }
            }
        }
    });
}

function update() {
    // $('#submit').prop('disabled', true);
    $.ajax({
        cache: true,
        type: "POST",
        url: "/jzweb/eventresourceAllocation/update",
        data: $('#signupForm').serialize(),// 你的formid
        async:false,
        error: function (request) {
            parent.layer.alert("Connection error");
            $('#submit').prop('disabled', false);
        },
        success: function (data) {
            if (data.code == 0) {
                // $('#submit').prop('disabled', false);
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            } else {
                // $('#submit').prop('disabled', false);
                parent.layer.alert(data.msg)
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            name: {
                required: true
            }
        },
        messages: {
            name: {
                required: icon + "请输入名字"
            }
        }
    })
}